package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system._enum.KfnWorkOrderMaAdidasUpdateMethodEnum;
import com.ruoyi.system.domain.KfnProject;
import com.ruoyi.system.domain.KfnWorkOrder;
import com.ruoyi.system.domain.bo.KfnWorkOrderBo;
import com.ruoyi.system.domain.vo.KfnProjectVo;
import com.ruoyi.system.domain.vo.KfnWorkOrderStoreAdidasVo;
import com.ruoyi.system.domain.vo.KfnWorkOrderVo;
import com.ruoyi.system.mapper.KfnProjectMapper;
import com.ruoyi.system.mapper.KfnWorkOrderMapper;
import com.ruoyi.system.mapper.KfnWorkOrderStoreAdidasMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.KfnWorkOrderMaAdidasBo;
import com.ruoyi.system.domain.vo.KfnWorkOrderMaAdidasVo;
import com.ruoyi.system.domain.KfnWorkOrderMaAdidas;
import com.ruoyi.system.mapper.KfnWorkOrderMaAdidasMapper;
import com.ruoyi.system.service.IKfnWorkOrderMaAdidasService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * adidas工单Service业务层处理
 *
 * @author Panda
 * @date 2023-12-21
 */
@RequiredArgsConstructor
@Service
public class KfnWorkOrderMaAdidasServiceImpl implements IKfnWorkOrderMaAdidasService {

    private final KfnWorkOrderMaAdidasMapper baseMapper;
    private final KfnWorkOrderMapper kfnWorkOrderMapper;
    private final KfnWorkOrderStoreAdidasMapper kfnWorkOrderStoreAdidasMapper;
    private final KfnProjectMapper kfnProjectMapper;
    /**
     * 查询adidas工单
     */
    @Override
    public KfnWorkOrderMaAdidasVo queryById(String workOrderId){
        return baseMapper.selectVoById(workOrderId);
    }

    /**
     * 查询adidas工单列表
     */
    @Override
    public TableDataInfo<KfnWorkOrderMaAdidasVo> queryPageList(KfnWorkOrderMaAdidasBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KfnWorkOrderMaAdidas> lqw = buildQueryWrapper(bo);
        Page<KfnWorkOrderMaAdidasVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<KfnWorkOrderMaAdidasVo> kfnWorkOrderMaAdidasVos = result.getRecords();
        //获取所有的工单号
        List<String> workOrderIds = kfnWorkOrderMaAdidasVos.stream().map(KfnWorkOrderMaAdidasVo::getWorkOrderId).collect(Collectors.toList());
        //通过所查询的工单号差工单池内通用信息
        if(CollectionUtil.isEmpty(workOrderIds)){
            return TableDataInfo.build(result);
        }
        Map<String, KfnWorkOrderVo> kfnWorkOrderVoMap = kfnWorkOrderMapper.selectVoBatchIds(workOrderIds).stream().collect(Collectors.toMap(KfnWorkOrderVo::getWorkOrderId, kfnWorkOrderVo -> kfnWorkOrderVo));
        //将通用信息复制到独立品牌的工单上
        kfnWorkOrderMaAdidasVos.forEach(k -> {
            KfnWorkOrderVo kfnWorkOrderVo = kfnWorkOrderVoMap.get(k.getWorkOrderId());
            BeanUtil.copyProperties(kfnWorkOrderVo,k,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        });
        result.setRecords(kfnWorkOrderMaAdidasVos);
        return TableDataInfo.build(result);
    }

    /**
     * 查询adidas工单列表
     */
    @Override
    public List<KfnWorkOrderMaAdidasVo> queryList(KfnWorkOrderMaAdidasBo bo) {
        LambdaQueryWrapper<KfnWorkOrderMaAdidas> lqw = buildQueryWrapper(bo);
        List<KfnWorkOrderMaAdidasVo> kfnWorkOrderMaAdidasVos = baseMapper.selectVoList(lqw);
        //获取所有的工单号
        List<String> workOrderIds = kfnWorkOrderMaAdidasVos.stream().map(KfnWorkOrderMaAdidasVo::getWorkOrderId).collect(Collectors.toList());
        //通过所查询的工单号差工单池内通用信息
        if(CollectionUtil.isEmpty(workOrderIds)){
            return kfnWorkOrderMaAdidasVos;
        }
        Map<String, KfnWorkOrderVo> kfnWorkOrderVoMap = kfnWorkOrderMapper.selectVoBatchIds(workOrderIds).stream().collect(Collectors.toMap(KfnWorkOrderVo::getWorkOrderId, kfnWorkOrderVo -> kfnWorkOrderVo));
        //将通用信息复制到独立品牌的工单上
        kfnWorkOrderMaAdidasVos.forEach(k -> {
            KfnWorkOrderVo kfnWorkOrderVo = kfnWorkOrderVoMap.get(k.getWorkOrderId());
            BeanUtil.copyProperties(kfnWorkOrderVo,k,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        });
        return kfnWorkOrderMaAdidasVos;

    }

    private LambdaQueryWrapper<KfnWorkOrderMaAdidas> buildQueryWrapper(KfnWorkOrderMaAdidasBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KfnWorkOrderMaAdidas> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getResponseLevel()), KfnWorkOrderMaAdidas::getResponseLevel, bo.getResponseLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getFaultClass()), KfnWorkOrderMaAdidas::getFaultClass, bo.getFaultClass());
        lqw.eq(StringUtils.isNotBlank(bo.getFaultCode()), KfnWorkOrderMaAdidas::getFaultCode, bo.getFaultCode());
        lqw.eq(StringUtils.isNotBlank(bo.getSoluteCode()), KfnWorkOrderMaAdidas::getSoluteCode, bo.getSoluteCode());
        lqw.eq(StringUtils.isNotBlank(bo.getFaultEquipment()), KfnWorkOrderMaAdidas::getFaultEquipment, bo.getFaultEquipment());
        return lqw;
    }

    /**
     * 新增adidas工单
     */
    @Transactional
    @Override
    public Boolean insertByBo(KfnWorkOrderMaAdidasBo bo) {
        KfnWorkOrderMaAdidas add = BeanUtil.toBean(bo, KfnWorkOrderMaAdidas.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setWorkOrderId(add.getWorkOrderId());
        }
        //查询店铺信息
        KfnWorkOrderStoreAdidasVo kfnWorkOrderStoreAdidasVo = kfnWorkOrderStoreAdidasMapper.selectVoById(bo.getStoreId());
        BeanUtil.copyProperties(kfnWorkOrderStoreAdidasVo,bo, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        bo.setStoreAddress(kfnWorkOrderStoreAdidasVo.getProvince()+kfnWorkOrderStoreAdidasVo.getCity()+kfnWorkOrderStoreAdidasVo.getDistricts()+kfnWorkOrderStoreAdidasVo.getAddress());
        //查询项目信息
        KfnProjectVo kfnProjectVo = kfnProjectMapper.selectVoById(bo.getProjectId());
        BeanUtil.copyProperties(kfnProjectVo,bo, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));

        //写入工单池
        KfnWorkOrder kfnWorkOrder = BeanUtil.toBean(bo, KfnWorkOrder.class);
        boolean insert = kfnWorkOrderMapper.insert(kfnWorkOrder)>0;

        if (insert && flag) {
            return true;
        } else {
            throw new RuntimeException("未知异常，回滚插入");
        }
    }

    /**
     * 修改adidas工单
     */
    @Transactional
    @Override
    public Boolean updateByBo(KfnWorkOrderMaAdidasBo bo) {
        KfnWorkOrderMaAdidas update = BeanUtil.toBean(bo, KfnWorkOrderMaAdidas.class);
        validEntityBeforeSave(update);

        if(bo.getMethodName().equals(KfnWorkOrderMaAdidasUpdateMethodEnum.UPDATE.getMethodName())) {
            //修改基础信息
            KfnWorkOrder kfnWorkOrder = BeanUtil.toBean(bo, KfnWorkOrder.class);
            kfnWorkOrderMapper.updateById(kfnWorkOrder);
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KfnWorkOrderMaAdidas entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除adidas工单
     */
    @Transactional
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        kfnWorkOrderMapper.deleteBatchIds(ids);
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
